package ar.com.solidussource.howmuchservices.web.controller;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ar.com.solidussource.framework.backend.bo.exception.BusinessException;
import ar.com.solidussource.framework.backend.dao.filter.Page;
import ar.com.solidussource.framework.backend.dao.filter.Result;
import ar.com.solidussource.framework.backend.dao.filter.TableOrder;
import ar.com.solidussource.howmuchservices.backend.bo.ProductcategoryCRUDBO;
import ar.com.solidussource.howmuchservices.backend.bo.ProductsubcategoryCRUDBO;
import ar.com.solidussource.howmuchservices.common.entities.Productcategory;
import ar.com.solidussource.howmuchservices.common.entities.Productsubcategory;
import ar.com.solidussource.howmuchservices.common.entities.filter.ProductsubcategoryFilter;
import ar.com.solidussource.howmuchservices.utils.json.JSONUtils;

@Controller
@RequestMapping("/productsubcategory")
public class ProductsubcategoryController extends BaseController {
	private static final Logger log = Logger.getLogger(ProductsubcategoryController.class);
	private static final String TABLEORDER_CRITERION = "name";
	private static final String TABLEORDER_DIRECTION = "desc";
	
	@Autowired ProductcategoryCRUDBO productcategoryCRUDBO;
	@Autowired ProductsubcategoryCRUDBO productsubcategoryCRUDBO;
	
	@RequestMapping(value="/list.json", method = RequestMethod.GET, produces="application/json")
	@ResponseBody
	public String getProductsubcategorys(HttpServletResponse httpServletResponse, @RequestParam Integer productcategoryId, @RequestParam Integer pageNumber, @RequestParam Integer size) {
		
		try {
			Productcategory productcategory = productcategoryCRUDBO.getById(productcategoryId);
			
			if (productcategory != null) {
				ProductsubcategoryFilter productsubcategoryFilter = new ProductsubcategoryFilter();
				productsubcategoryFilter.setProductcategoryId(productcategoryId);
				productsubcategoryFilter.setTableOrder(new TableOrder(TABLEORDER_CRITERION, TABLEORDER_DIRECTION));
				
				Page page = new Page(pageNumber, size);
				
				Result<Productsubcategory> productsubcategorys = productsubcategoryCRUDBO.getAll(productsubcategoryFilter, page);
				
				return JSONUtils.getJSON(productsubcategorys.getResult());
				
			} else {
				throw new BusinessException("no productcategory "+productcategoryId);
			}
			
		} catch (Exception e) {
			httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			log.error(e.getMessage(), e);			
			return getJsonStatus(e.getMessage());
		}
 
	}
	
	@RequestMapping(value="/create.json", method = RequestMethod.PUT, produces="application/json")
	@ResponseBody
	public String createProductsubcategory(HttpServletResponse httpServletResponse, @RequestParam Integer productcategoryId, @RequestParam String name, @RequestParam(required = false) String description) {
		
		try {
			Productcategory productcategory = productcategoryCRUDBO.getById(productcategoryId);
			
			if (productcategory != null) {
				Productsubcategory productsubcategory = new Productsubcategory();
				productsubcategory.setProductcategory(productcategory);
				productsubcategory.setName(name);
				productsubcategory.setDescription(description);
				
				productsubcategoryCRUDBO.saveNew(productsubcategory);
				
				return getJsonStatus(RESPONSE_OK);
				
			} else {
				throw new BusinessException("no productcategory "+productcategoryId);
			}
			
		} catch (Exception e) {
			httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			log.error(e.getMessage(), e);			
			return getJsonStatus(e.getMessage());
		}
 
	}	
}
